package com.test.memoryapp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;

public class AndroidFileBrowser extends ListActivity{
	private File currentDirectory = new File("/");
	private List<String> directoryEntries = new ArrayList<String>();
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		browseToRoot();
	}
	
	private void browseToRoot() {
		browseTo(new File("/mnt/sdcard/"));
    }
	
	private void browseTo(final File aDirectory){
		if (aDirectory.isDirectory()){
			this.currentDirectory = aDirectory;
			fill(aDirectory.listFiles());
		}else{
			OnClickListener okButtonListener = new OnClickListener(){
				// @Override
				public void onClick(DialogInterface arg0, int arg1) {
					
				}

				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					
				}
			};
			OnClickListener cancelButtonListener = new OnClickListener(){
				// @Override
				public void onClick(DialogInterface arg0, int arg1) {
					// Do nothing
				}

				public void onClick(View v) {
					// TODO Auto-generated method stub
					
				}
			};
			
		}
	}
	
	private void fill(File[] files) {
		this.directoryEntries.clear();
		
		try {
			Thread.sleep(10);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		this.directoryEntries.add(".");
		
		if(this.currentDirectory.getParent() != null)
			this.directoryEntries.add("..");
		
		for (File file : files){
			this.directoryEntries.add(file.getPath());
		}
		ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
				R.layout.file_row, this.directoryEntries);
		
		this.setListAdapter(directoryList);
	}
}
